﻿using System;
using Mbs.ComponentModel.Container.Activation;
using Mbs.ComponentModel.Container.Metadata;

namespace Mbs.ComponentModel.Container.Lifestyle
{
    [Serializable]
    public class ProxyLifestyleManager : ILifestyleManager
    {
        private readonly ILifestyleManager Real;

        public IMicroKernel Kernel { get { return Real.Kernel; } }
        public IActivator Activator { get { return Real.Activator; } }
        public IComponentInfo Info { get { return Real.Info; } }
        public Action<IComponentInfo, object> OnDestroyed { get { return Real.OnDestroyed; } }

        public ProxyLifestyleManager(ILifestyleManager real)
        {
            Real = real;
            Init(real.Activator, real.Kernel, real.Info, real.OnDestroyed);
        }

        public void Init(Mbs.ComponentModel.Container.Activation.IActivator activator, IMicroKernel kernel, Mbs.ComponentModel.Container.Metadata.IComponentInfo info, Action<Mbs.ComponentModel.Container.Metadata.IComponentInfo, object> onDestroyed)
        {
            Real.Init(new ProxyActivator(activator), kernel, info, onDestroyed);
        }

        public void Dispose()
        {
            Real.Dispose();
        }

        public object Get(Mbs.ComponentModel.Container.Context.ICreateContext ctx)
        {
            return Real.Get(ctx);
        }

        public bool Release()
        {
            return Real.Release();
        }

        public bool Release(object instance)
        {
            return Real.Release(instance);
        }
    }
}
